package com.cadre.web.controller.officer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cadre.common.enums.ConferenceStateEnum;
import com.cadre.common.utils.R;
import com.cadre.common.utils.SecurityUtils;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.system.domain.officer.*;
import com.cadre.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = {"A-N上会批次接口"})
@RequestMapping({"/profess/batchconference_n"})
@RestController
/* loaded from: NEWBatchconferenceController.class */
public class NEWBatchconferenceController {
    @Autowired
    private NEWBatchconferenceService batchconferenceService;
    @Autowired
    private NEWConfernamelistService confernamelistService;
    @Autowired
    private NEWPostchangeService postchangeService;
    @Autowired
    private A19Service a19Service;
    @Autowired
    private A01Service a01Service;

    @PostMapping({"/getOftenWord"})
    @ApiOperation("常委会任免公示生成word")
    public void getOftenWord(String oftenBathid, HttpServletResponse response) {
        this.batchconferenceService.getOftenWord(oftenBathid, response);
    }

    public R getMeetName() {
        return R.ok().put("name", (Object) this.batchconferenceService.getMeetName());
    }

    @PostMapping({"/getMeetOftenName"})
    @ApiOperation("获取常委会新名称")
    public R getMeetOftenName() {
        return R.ok().put("name", (Object) this.batchconferenceService.getMeetOftenName());
    }

    @PostMapping({"/list"})
    @ApiOperation("查询上会批次")
    public R list(@RequestParam Map<String, Object> stata) {
        return R.ok().put("page", (Object) this.batchconferenceService.queryPage(stata));
    }

    @PostMapping({"/listForHistory"})
    @ApiOperation("查询历史上会批次")
    public R listForHistory(@RequestParam Map<String, Object> stata) {
        return R.ok().put("page", (Object) this.batchconferenceService.queryPageForHistory(stata));
    }

    @RequestMapping({"/info/{flowid}"})
    public R info(@PathVariable("flowid") String flowid) {
        return R.ok().put("batchconference", (Object) ((BatchconferenceEntity) this.batchconferenceService.getBaseMapper().selectById(flowid)));
    }

    @PostMapping({"/save"})
    @ApiOperation("新增上会批次")
    public R save(@RequestBody BatchconferenceEntity batchconference) {
        batchconference.setCreatetime(new Date());
        batchconference.setStata(ConferenceStateEnum.NOT_SUMMARIZED.getValue());
        this.batchconferenceService.save(batchconference);
        return R.ok();
    }

    @PostMapping({"/toBeAttended"})
    @ApiOperation("待上会列表")
    public R toAttendedlist() {
        return R.ok().put("data", (Object) this.batchconferenceService.getBaseMapper().selectList(
                new LambdaQueryWrapper<BatchconferenceEntity>().eq(BatchconferenceEntity::getStata, ConferenceStateEnum.TO_BE_ATTENDED.getValue())));
    }

    @PostMapping({"/notSummarized"})
    @ApiOperation("未汇总批次")
    public R notSummarized() {
        // （已调整完毕）todo:需要将sql迁移到xml中
        return R.ok().put("data", (Object) this.batchconferenceService.getBaseMapper().selectList(
                new LambdaQueryWrapper<BatchconferenceEntity>().select(BatchconferenceEntity::getFlowid,BatchconferenceEntity::getName)
                        .eq(BatchconferenceEntity::getStata, ConferenceStateEnum.NOT_SUMMARIZED.getValue())));
    }

    @PostMapping({"checkSummarzid"})
    @ApiOperation("校验是否已全部汇总")
    public R checkSummarzid() {
        boolean b = true;
        List<String> l = new ArrayList<>();
        l.add(ConferenceStateEnum.BE_SUMMARIZED.getValue());
        l.add(ConferenceStateEnum.NOT_SUMMARIZED.getValue());
        List<String> allIds = (List) this.batchconferenceService.getBaseMapper().selectList(
                new LambdaQueryWrapper<BatchconferenceEntity>().in(BatchconferenceEntity::getStata, l).eq(BatchconferenceEntity::getType, 0)).stream().map((v0) -> {
            return v0.getFlowid();
        }).collect(Collectors.toList());
        allIds.removeAll((List) this.confernamelistService.getBaseMapper().selectList(
                new LambdaQueryWrapper<ConfernamelistEntity>().isNotNull(ConfernamelistEntity::getOftenresult).in(ConfernamelistEntity::getBatchid, allIds)).stream().map((v0) -> {
            return v0.getBatchid();
        }).distinct().collect(Collectors.toList()));
        if (!CollectionUtils.isEmpty(allIds)) {
            b = false;
        }
        return R.ok().put("ischeck", (Object) b);
    }

    @PostMapping({"/summarzid/{name}"})
    @ApiOperation("汇总批次")
    public R summarzid(@PathVariable("name") String name, @RequestBody String[] ids) {
        Long deptId = SecurityUtils.getDeptId();
        String id = GenerateUUID.getUUID();
        BatchconferenceEntity b = new BatchconferenceEntity();
        b.setFlowid(id);
        b.setName(name);
        b.setUnitid(String.valueOf(deptId));
        b.setType("1");
        b.setUnitid(String.valueOf(deptId));
        b.setStata(ConferenceStateEnum.TO_BE_ATTENDED.getValue());
        b.setCreatetime(new Date());
        this.batchconferenceService.save(b);
        this.confernamelistService.updateOftenbatchId(id, ids);
        return R.ok().put("batchid", (Object) id);
    }

    @RequestMapping({"/update"})
    public R update(@RequestBody BatchconferenceEntity batchconference) {
        this.batchconferenceService.updateById(batchconference);
        return R.ok();
    }

    @PostMapping({"/deleteBybw"})
    @Transactional
    @ApiOperation("部务会批次删除")
    public R deleteBybw(@RequestBody String[] flowids) {
        this.batchconferenceService.removeBatchByIds(Arrays.asList(flowids));
        for (String batid : flowids) {
            for (ConfernamelistEntity entity : this.confernamelistService.getBaseMapper().selectList(
                    new LambdaQueryWrapper<ConfernamelistEntity>().eq(ConfernamelistEntity::getBatchid, batid))) {
                if (entity.getOftenbatchid() == null) {
                    this.confernamelistService.removeById(entity.getFlowid());
                    this.postchangeService.remove(new LambdaQueryWrapper<PostchangeEntity>().eq(PostchangeEntity::getNamelistid, entity.getFlowid()));
                } else if (((BatchconferenceEntity) this.batchconferenceService.getById(entity.getOftenbatchid())) == null) {
                    this.confernamelistService.removeById(entity.getFlowid());
                    this.postchangeService.remove(new LambdaQueryWrapper<PostchangeEntity>().eq(PostchangeEntity::getNamelistid, entity.getFlowid()));
                }
            }
        }
        return R.ok();
    }

    @PostMapping({"/deleteBycw"})
    @Transactional
    @ApiOperation("常委会批次删除")
    public R deleteBycw(@RequestBody String[] flowids) {
        this.batchconferenceService.removeBatchByIds(Arrays.asList(flowids));
        for (String batid : flowids) {
            for (ConfernamelistEntity entity : this.confernamelistService.getBaseMapper().selectList(
                    new LambdaQueryWrapper<ConfernamelistEntity>().eq(ConfernamelistEntity::getOftenbatchid, batid))) {
                if (((BatchconferenceEntity) this.batchconferenceService.getBaseMapper().selectById(entity.getFlowid())) == null) {
                    this.confernamelistService.removeById(entity.getFlowid());
                    this.postchangeService.remove(new LambdaQueryWrapper<PostchangeEntity>().eq(PostchangeEntity::getNamelistid, entity.getFlowid()));
                }
            }
        }
        return R.ok();
    }

    public void updateStata(String type, String flowid) {
        BatchconferenceEntity batchconference = new BatchconferenceEntity();
        batchconference.setFlowid(flowid);
        if (type != null && type != "") {
            if ("1".equals(type)) {
                batchconference.setStata(ConferenceStateEnum.TO_BE_ATTENDED.getValue());
            } else if ("2".equals(type)) {
                batchconference.setStata(ConferenceStateEnum.BE_ATTENDED.getValue());
            }
        }
    }

    public void chectMeetState(String batchid) {
        BatchconferenceEntity batchconferenceEntity = new BatchconferenceEntity();
        batchconferenceEntity.setFlowid(batchid);
        batchconferenceEntity.setConferencetime(new Date());
        batchconferenceEntity.setStata(ConferenceStateEnum.BE_ATTENDED.getValue());
        this.batchconferenceService.updateByPrimaryKeySelective(batchconferenceEntity);
    }

    public void chectMeetStateByBw(String batchid) {
        BatchconferenceEntity batchconferenceEntity = new BatchconferenceEntity();
        batchconferenceEntity.setFlowid(batchid);
        batchconferenceEntity.setConferencetime(new Date());
        batchconferenceEntity.setStata(ConferenceStateEnum.BE_SUMMARIZED.getValue());
        this.batchconferenceService.updateByPrimaryKeySelective(batchconferenceEntity);
    }

    public String pstring(String batchid, String type) {
        String perid;
        A01 a01;
        String pString = "";
        Map<String, Object> map = new HashMap<>();
        if ("1".equals(type)) {
            map.put("batchid", batchid);
        } else if ("2".equals(type)) {
            map.put("oftenbatchid", batchid);
        }
        List<ConfernamelistEntity> list = this.confernamelistService.selectByMaps(map);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (!(!"0".equals(list.get(i).getInputtype()) || (perid = list.get(i).getPersonid()) == null || perid == "" || (a01 = (A01) this.a01Service.getById(perid)) == null)) {
                    pString = pString + (a01.getA0101() + ",");
                }
            }
        }
        if (!(pString == null || pString == "")) {
            pString = pString + "拟任职务或拟免职务不完整。";
        }
        return pString;
    }

    @GetMapping({"/upEnd"})
    @ApiOperation("上会结束")
    public R upEnd(String batchid, String type, String uptime) {
        if (type.equals("1")) {
            chectMeetStateByBw(batchid);
        } else if (type.equals("2")) {
            chectMeetState(batchid);
        }
        return R.ok();
    }

    public void upA01Datas(String batchid, String type) {
        if (!(batchid == null || batchid == "")) {
            Date nowtime = null;
            BatchconferenceEntity batchconferenceEntity = (BatchconferenceEntity) this.batchconferenceService.getById(batchid);
            if (batchconferenceEntity != null) {
                nowtime = batchconferenceEntity.getConferencetime();
            }
            List<ConfernamelistEntity> list = new ArrayList();
            if ("2".equals(type)) {
                Map<String, Object> map = new HashMap<>();
                map.put("oftenbatchid", batchid);
                list = this.confernamelistService.selectByMaps(map);
            } else if ("1".equals(type)) {
                Map<String, Object> map2 = new HashMap<>();
                map2.put("batchid", batchid);
                list = this.confernamelistService.selectByMaps(map2);
            }
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    String a0177b = ((A01) this.a01Service.getById(list.get(i).getPersonid())).getA0177b();
                    String meettime = DateUtil.dateToString(nowtime, DateUtil.MONTG_DATE_FORMAT);
                    String Assumepost = list.get(i).getAssumepost();
                    if (!(Assumepost == null || Assumepost == "" || meettime == null || meettime == "")) {
                        a0177b = getA0177b(a0177b, meettime, Assumepost);
                    }
                    A01 a01 = new A01();
                    a01.setA0183(list.get(i).getAssumepost());
                    a01.setFlowid(list.get(i).getPersonid());
                    a01.setA0185(nowtime);
                    a01.setA0177b(a0177b);
                    this.a01Service.updateByPrimaryKeySelective(a01);
                }
            }
        }
    }

    public String getA0177b(String a0177b, String meettime, String Assumepost) {
        int boo = a0177b.lastIndexOf("--");
        StringBuilder sb = new StringBuilder(a0177b);
        sb.replace(boo + 2, boo + 6, meettime);
        String a0177b2 = sb.toString() + ("\n" + meettime + "--         " + Assumepost);
        System.out.println("@@@@@@@@@@@@@@@@@@@" + a0177b2 + "$$$$$$$$$$$$$$$$$$$");
        return a0177b2;
    }

    public void upA19Datas(String batchid, String type) {
        if (!(batchid == null || batchid == "")) {
            List<ConfernamelistEntity> list = new ArrayList();
            if ("2".equals(type)) {
                Map<String, Object> map = new HashMap<>();
                map.put("oftenresult", ConferenceStateEnum.BE_ATTENDED.getValue());
                map.put("oftenbatchid", batchid);
                list = this.confernamelistService.selectByMaps(map);
            } else if ("1".equals(type)) {
                Map<String, Object> map2 = new HashMap<>();
                map2.put("result", ConferenceStateEnum.TO_BE_ATTENDED.getValue());
                map2.put("batchid", batchid);
                list = this.confernamelistService.selectByMaps(map2);
            }
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("personid", list.get(i).getPersonid().toString());
                    map1.put("namelistid", batchid);
                    List<PostchangeEntity> postchangeEntities = this.postchangeService.selectByMaps(map1);
                    if (postchangeEntities.size() > 0) {
                        for (int j = 0; j < postchangeEntities.size(); j++) {
                            A19 a19 = new A19();
                            a19.setFlowid(postchangeEntities.get(j).getA19flowid());
                            a19.setA1905(postchangeEntities.get(j).getA1905());
                            a19.setA1910(postchangeEntities.get(j).getA1910());
                            a19.setA1915(postchangeEntities.get(j).getA1915());
                            a19.setA1916(postchangeEntities.get(j).getA1916());
                            a19.setA1920(postchangeEntities.get(j).getA1920());
                            a19.setA1925(postchangeEntities.get(j).getA1925());
                            a19.setA1930(postchangeEntities.get(j).getA1930());
                            a19.setA1935(postchangeEntities.get(j).getA1935());
                            a19.setA1940(postchangeEntities.get(j).getA1940());
                            a19.setA1945(postchangeEntities.get(j).getA1945());
                            a19.setA1950(postchangeEntities.get(j).getA1950());
                            a19.setA1955(postchangeEntities.get(j).getA1955());
                            a19.setA1956(postchangeEntities.get(j).getA1956());
                            a19.setA1960(postchangeEntities.get(j).getA1960());
                            a19.setA1965(postchangeEntities.get(j).getA1965());
                            a19.setA1970(postchangeEntities.get(j).getA1970());
                            a19.setPersonid(postchangeEntities.get(j).getPersonid());
                            a19.setRemark(postchangeEntities.get(j).getRemark());
                            a19.setSerial(postchangeEntities.get(j).getSerial());
                            a19.setSort(postchangeEntities.get(j).getSort());
                            if (postchangeEntities.get(j).getFlag().shortValue() == 0) {
                                a19.setFlowid(GenerateUUID.getUUID());
                                this.a19Service.save(a19);
                            } else {
                                this.a19Service.updateByPrimaryKeySelective(a19);
                            }
                        }
                    }
                }
            }
        }
    }
}
